#include "Bullet.h"
#include "GamePlayState.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "DestroyEntityMessage.h"
#include <sstream>
#include <random>
#include "Game.h"

Bullet::Bullet(std::string en, std::string en2)
{
	emitter = en;
	explodeemitter = en2;
	activeemitter = ParticleManager::GetInstance()->CreateEmitter(emitter, this);
	std::random_device rd; //create the random device seed
	std::mt19937 mt(rd()); //create the random engine and seed it with the device
	std::uniform_real_distribution<float> randRadius(0.5f, 1.5f); //random radius generator for the new planet
	timer = .5f;//randRadius(mt);
}


Bullet::~Bullet()
{
	if (activeemitter)
		ParticleManager::GetInstance()->FreeEmitter(activeemitter);
	if (activeemitter2)
		ParticleManager::GetInstance()->FreeEmitter(activeemitter);
}

void Bullet::Update(float elapsedTime)
{
	SGD::Rectangle camOffset{ { Game::GetInstance()->GetCameraPos().x - 100,
		Game::GetInstance()->GetCameraPos().y - 100 },
		SGD::Size{ (Game::GetInstance()->GetScreenWidth() + 500) / Game::GetInstance()->GetZoomLevel(),
		(Game::GetInstance()->GetScreenHeight() + 500) / Game::GetInstance()->GetZoomLevel() } };
	SGD::Rectangle posRect = { this->GetRect().ComputeCenter(), SGD::Size{ 1, 1 } };
	if (camOffset.IsIntersecting(posRect))
	{
		collisionFlag = 0;
		timer -= elapsedTime;
		if (timer <= 0)
		{
			exploded = true;
			SGD::Vector zero = SGD::Vector{ 0, 0 };
			SetVelocity(zero);
			if (exploded&&waitExplode == 1)
			{
				ParticleManager::GetInstance()->FreeEmitter(activeemitter);
				activeemitter2 = ParticleManager::GetInstance()->CreateEmitter(explodeemitter, this);
				SetRadius((GetRadius() + range));
				SetSize({ range * 2, range * 2 });
				CalculateRadius();
				SetPosition({ position.x - range, position.y - range });
			}
			waitExplode -= elapsedTime;
		}
		if (waitExplode <= 0)
		{
			ParticleManager::GetInstance()->FreeEmitter(activeemitter2);
			activeemitter = 0;
			DestroyEntityMessage* msg = new DestroyEntityMessage(this);
			msg->QueueMessage();
			msg = nullptr;
		}
		Entity::Update(elapsedTime);
	}
	else
		collisionFlag = -1;
}

void Bullet::Render(void)
{
	SGD::Rectangle camOffset{ { Game::GetInstance()->GetCameraPos().x - 100,
		Game::GetInstance()->GetCameraPos().y - 100 },
		SGD::Size{ (Game::GetInstance()->GetScreenWidth() + 500) / Game::GetInstance()->GetZoomLevel(),
		(Game::GetInstance()->GetScreenHeight() + 500) / Game::GetInstance()->GetZoomLevel() } };
	SGD::Rectangle posRect = { this->GetRect().ComputeCenter(), SGD::Size{ 1, 1 } };
	if (camOffset.IsIntersecting(posRect))
	{
		SGD::Rectangle rect;
		//AOE radius
		rect = { GetRect().ComputeCenter().x - range - Game::GetInstance()->GetCameraPos().x, GetRect().ComputeCenter().y - range - Game::GetInstance()->GetCameraPos().y,
			GetRect().ComputeCenter().x + range - Game::GetInstance()->GetCameraPos().x, GetRect().ComputeCenter().y + range - Game::GetInstance()->GetCameraPos().y };
		// bullet
		rect = { SGD::Point{ position.x - Game::GetInstance()->GetCameraPos().x,
			position.y - Game::GetInstance()->GetCameraPos().y }, size };
		//SGD::GraphicsManager::GetInstance()->DrawRectangle(rect, SGD::Color{ 0, 255, 0 });
		//bullet vel
	}
}
void Bullet::HandleCollision(const IEntity* other)
{
	if (GetOwner() != other && (other->GetType() != ENT_PLANET))
	{
		ParticleManager::GetInstance()->FreeEmitter(activeemitter);
		ParticleManager::GetInstance()->FreeEmitter(activeemitter2);
		activeemitter = 0;
		activeemitter2 = 0;
		DestroyEntityMessage* msg = new DestroyEntityMessage(this);
		msg->QueueMessage();
		msg = nullptr;
	}
}